#include "sprite.h"

namespace sprites
{
	Sprite::Sprite(Vector3D _pos, MatterType _type, float boundRadius, float _scale) 
		: pos(_pos), type(_type), scale(_scale)
	{
		bound = BindingSphere(_pos, boundRadius);
		
		//used for rotation
		horzAngle = 0.0f;
		vertAngle = 0.0f;
		forwardAngle = 0.0f;
		dead = false;

		//dummy nodes used to rotate on the 3 axis
		horzNode    = new ParentNode();
		vertNode    = new ParentNode();
		forwardNode = new ParentNode();
		root        = new ParentNode();

		horzNode->addChild(forwardNode);
		forwardNode->addChild(vertNode);
		vertNode->addChild(root);

		parent.addChild(horzNode);

		parent+=pos;
		root->setScale(scale, scale, scale);
	};

	//helper function
	void Sprite::setVector(float * vector, float x, float y, float z)
	{
		vector[0] = x;
		vector[1] = y;
		vector[2] = z;
	}

	//used for collision handling
	MatterType Sprite::getMatterType()
	{
		return type;
	}

	//rotation methods
	void Sprite::rotateHorizontal( float _degrees ){
		float axis[] = { 0.0f, 1.0f, 0.0f};
		horzAngle += _degrees;
		horzNode->setAxisAngle(axis, horzAngle);
	}
	
	void Sprite::rotateVertical( float _degrees ){
		float axis[] = { 0.0f, 0.0f, 1.0f };
		vertAngle += _degrees;
		vertNode->setAxisAngle(axis, vertAngle);
	}

	void Sprite::rotateForward( float _degrees ){
		float axis[] = {1.0f, 0.0f, 0.0f};
		forwardAngle += _degrees;
		forwardNode->setAxisAngle(axis, forwardAngle);
	}

	void Sprite::resetRotation()
	{
		float axis[] = {1.0f, 0.0f, 0.0f};
		forwardAngle = 0;
		forwardNode->setAxisAngle(axis, forwardAngle);
		
		float axis2[] = {0.0f, 1.0f, 0.0f};
		horzAngle = 0;
		horzNode->setAxisAngle(axis2, horzAngle);
		
		float axis3[] = {0.0f, 0.0f, 1.0f};
		vertAngle = 0;
		vertNode->setAxisAngle(axis3, vertAngle);
	}

	//operator overloading
	void Sprite::operator +=(Vector3D _move)
	{
		parent+=_move;
	}

	void Sprite::draw()
	{
		if(!dead)
			parent.draw();
	}

	bool Sprite::isDead()
	{
		return dead;
	}

	void Sprite::revive()
	{
		dead = false;
	}

	Vector3D Sprite::getPosition()
	{
		return parent.getTranslation();
	}
	void Sprite::setPosition(collisions::Vector3D _position)
	{
		parent.setTranslation(_position);
	}
}